package com.outfood.api.controller;

import com.outfood.common.common.CurrentUser;
import com.outfood.common.common.replay.ReplyCode;
import com.outfood.common.common.response.RestResponse;
import com.outfood.config.WxConfig;
import com.outfood.domain.User;
import com.outfood.dto.LoginDTO;
import com.outfood.service.IUserService;
import com.outfood.service.IUserStoreRelationService;
import com.outfood.vo.LoginVO;
import com.rys.common.core.domain.AjaxResult;
import com.rys.common.utils.Search;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

@Log4j2
@RestController
@RequestMapping("api/user")
@Api(value = "UserController" ,tags = {"用户操作类"})
public class UserController {

    @Autowired
    IUserService userService;
    @Resource
    WxConfig wxConfig;
    @Resource
    IUserStoreRelationService userStoreRelationService;

    /**
     * 小程序登录
     * @param loginDTO
     * @return
     */
    @RequestMapping("/login")
    public RestResponse<LoginVO> login(LoginDTO loginDTO){
        LoginVO login = userService.login(loginDTO);
        return RestResponse.success(login);
    }

    @ApiOperation("获取状态")
    @RequestMapping("/getisTesting")
    public RestResponse<Boolean> getisTesting(){
        return RestResponse.success(wxConfig.getIsTesting());
    }


    @ApiOperation("获取当前用户的ID")
    @RequestMapping("/getUser")
    public RestResponse getUserId(){
        User user = userService.get(CurrentUser.getUserId());
        return RestResponse.success(user);
    }

    @ApiOperation("设置推荐人")
    @RequestMapping("/setReferenceId")
    public RestResponse setReferenceId(@ApiParam("推荐人ID")@RequestParam Long referenceId){
        try {
            User user = userService.get(CurrentUser.getUserId());
            //判断用户有没有绑定过门店
            if(new Date().getTime() - (1000*60) < user.getCreateDate().getTime()){
                log.warn("该用户是新用户，开始设置推荐人信息");
                user.setReferenceId(referenceId);
                userService.update(user.getId(),user,referenceId+"");
            }else{
                log.warn("该用户不是新用户");
            }
        } catch (Exception e) {
            log.error("",e);
        }
        return new RestResponse(ReplyCode.SYS.SUCCESS,"操作成功");
    }
}
